//  Converts feedback into html
//
//	Copyright (c) 2009, Krzysztof Langner
//	This code is released under MIT license

//-------------------------------------------------------------------------------------------------
function create_feedback(input){
   	var feedback = new Feedback();
   	var	options = input.getElementsByTagName("score");
   	
   	for(var i = 0; i < options.length; i++){
   		feedback.addOption(parseInt(options[i].get("points")), options[i].innerHTML);
   	}
   	
   	return feedback;
}

var Feedback = new Class({
	// constructor
    initialize: function(){
		this.options = new Array();
        this.domNode = document.createElement("p");
    },

    // show answers
    reset: function(){
    	// remove old nodes
    	while( this.domNode.hasChildNodes() ){
    		this.domNode.removeChild(this.domNode.firstChild);
    	}
    },
    
    // Show feedback
    showFeedback: function(result, maxScore){
    	// remove old nodes
    	this.reset();
    	
    	// find option
    	for(var i = 0; i < this.options.length; i++){
    		if(this.options[i]['points'] == result){
    			this.domNode.innerHTML = this.options[i]['html'];
    			break;
    		}
    	}
    },

    // get DOM
    getDOM: function(){
    	var root = this.domNode;
   		return root;
    },

    
    // ---------------------------------------------------------------------------------------------
    // Private implementation
    // Add option to choice
    addOption: function(points, innerHtml){
    	var option = new Array();

    	option["points"] = points;
    	option["html"] = innerHtml;

    	this.options.push(option);
    }

});

